package com.heaboy.emaidemo1.util;

import com.heaboy.emaidemo1.config.MailConst;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailService {

    /**
     * 发送简单文本邮件
     */
    public static boolean sendSimpleEmail(String toEmail, String title, String content) {
        try {
            JavaMailSender mailSender = createMailSender(toEmail);
            SimpleMailMessage message = new SimpleMailMessage();

            // 设置发件人邮箱和显示名称
            String fromEmail = toEmail.endsWith("@qq.com") ?
                    MailConst.QQ_USER_NAME : MailConst.USER_NAME;
            message.setFrom(fromEmail);

            message.setTo(toEmail);
            message.setSubject(title);
            message.setText(content);

            mailSender.send(message);
            System.out.println("邮件发送成功！目标邮箱: " + toEmail);
            return true;
        } catch (Exception e) {
            System.out.println("邮件发送失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 发送HTML格式邮件
     */
    public static boolean sendHtmlEmail(String toEmail, String title, String htmlContent) {
        try {
            JavaMailSender mailSender = createMailSender(toEmail);
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            // 设置发件人邮箱和显示名称
            String fromEmail = toEmail.endsWith("@qq.com") ?
                    MailConst.QQ_USER_NAME : MailConst.USER_NAME;
            String displayName = MailConst.FROM_DISPLAY_NAME;

            helper.setFrom(fromEmail, displayName);
            helper.setTo(toEmail);
            helper.setSubject(title);
            helper.setText(htmlContent, true); // true表示内容为HTML

            mailSender.send(message);
            System.out.println("HTML邮件发送成功！目标邮箱: " + toEmail);
            return true;
        } catch (Exception e) {
            System.out.println("HTML邮件发送失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 根据收件人邮箱创建合适的邮件发送器
     */
    private static JavaMailSender createMailSender(String toEmail) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        if (toEmail.endsWith("@qq.com")) {
            // QQ邮箱配置
            mailSender.setHost(MailConst.QQ_MAIL_HOST);
            mailSender.setPort(465);
            mailSender.setUsername(MailConst.QQ_USER_NAME);
            mailSender.setPassword(MailConst.QQ_PWD_CODE);
        } else {
            // 163邮箱配置
            mailSender.setHost(MailConst.MAIL_HOST);
            mailSender.setPort(465);
            mailSender.setUsername(MailConst.USER_NAME);
            mailSender.setPassword(MailConst.PWD_CODE);
        }

        // 通用配置
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.connectiontimeout", 5000);
        props.put("mail.smtp.timeout", 5000);
        props.put("mail.smtp.writetimeout", 5000);

        return mailSender;
    }

    /**
     * 带重试机制的邮件发送
     */
    public static boolean sendEmailWithRetry(String toEmail, String title, String content, int maxRetries) {
        for (int i = 0; i < maxRetries; i++) {
            boolean success = sendSimpleEmail(toEmail, title, content);
            if (success) {
                return true;
            }

            // 失败后等待一段时间再重试
            try {
                Thread.sleep(2000 * (i + 1)); // 等待时间逐渐增加
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return false;
            }
        }
        return false;
    }
}